package com.thinking.in.spring.ioc.dependency.injection;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * {@link org.springframework.beans.factory.Aware} 接口回调的依赖注入
 *
 *      实现 Aware 相关的接口，将 Spring 内建的一些  Bean 给注入
 * 回调的注入方式 ：
 * @author zjiajia
 * @date 2022/1/2 16:19
 */
public class AwareDependencyInjectionDemo implements BeanFactoryAware , ApplicationContextAware {


    static BeanFactory beanFactory;
    static ApplicationContext applicationContext;


    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        applicationContext.register(AwareDependencyInjectionDemo.class);


        applicationContext.refresh();




        System.out.println("AwareDependencyInjectionDemo -> beanFactory " + AwareDependencyInjectionDemo.beanFactory);
        System.out.println("AwareDependencyInjectionDemo -> applicationContext " +
                ( AwareDependencyInjectionDemo.applicationContext));

        System.out.println("application ==  AwareDependencyInjectionDemo -> applicationContext" + (applicationContext == AwareDependencyInjectionDemo.applicationContext));
        applicationContext.close();
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        AwareDependencyInjectionDemo.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AwareDependencyInjectionDemo.applicationContext = applicationContext;
    }
}
